package org.lanqiao.netctoss.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.lanqiao.netctoss.dao.AdminDao;
import org.lanqiao.netctoss.dao.AdperDao;
import org.lanqiao.netctoss.dao.PermissDao;
import org.lanqiao.netctoss.entity.Admin;
import org.lanqiao.netctoss.entity.Permiss;
import org.lanqiao.netctoss.service.AdminService;
import org.lanqiao.netctoss.util.GetSession;


public class AdminServiceImpl implements AdminService {

	@Override
	public List<Admin> selectAdmin() {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		/*List<Admin> la = new ArrayList<Admin>();
		List<Permiss> lp = new ArrayList<Permiss>();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		la = adminDao.selectAdmin();
		PermissDao permissDao = session.getMapper(PermissDao.class);
		for(Admin admin:la){
			lp = permissDao.selectPermissByAdmin(admin);
			admin.setLp(lp);
		}*/
		List<Admin> la=session.getMapper(AdminDao.class).selectAdmin();
		if(la!=null){
			session.commit();
			GetSession.close();
		}
		return la;
		
	}

	@Override
	public int updatePsw(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		int i = adminDao.updatePsw(admin);
		session.commit();
		GetSession.close();
		return i;
	}

	@Override
	public int updateAdmin(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		int i = adminDao.updateAdmin(admin);
		session.commit();
		GetSession.close();
		return i;
	}

	@Override
	public int insertAdmin(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		int a = adminDao.insertAdmin(admin);
		int b = adminDao.insertAdper(admin);
		int x =0;
		if(a>0&&b>0){
			session.commit();
			GetSession.close();
			x=1;
		}else session.rollback();
		return x;
	}

	@Override
	public int insertAdper(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		int b = adminDao.insertAdper(admin);
		if(b>0){
			session.commit();
			GetSession.close();
		}
		return b;
	}

	@Override
	public int deleteAdmin(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);		
		int a= adminDao.deleteAdmin(admin);
		if(a>0){
			session.commit();
			GetSession.close();
		}
		return a;
	}

	@Override
	public List<Admin> selectAdminByno(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		List<Admin> la= adminDao.selectAdminByno(admin);
		if(la!=null){
			session.commit();
			GetSession.close();
		}
		return la;
	}

	@Override
	public Admin login(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		Admin admin1 = adminDao.login(admin);
		session.close();
		return admin1;
	}

	@Override
	public boolean deleteAdperByAno(Admin admin) {
		// TODO Auto-generated method stub
		SqlSession session = GetSession.getSession();
		AdminDao adminDao = session.getMapper(AdminDao.class);
		int i = adminDao.deleteAdperByAno(admin);
		if(i>0){
			session.commit();
			session.close();
			return true;
		}
		return false;
	}
}
